package com.jp.kefu.web.handler;

import com.jp.kefu.web.message.AbstractMessage;
import com.jp.kefu.web.message.common.CommonMessage;
import com.jp.kefu.web.message.common.TextMessage;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lijin
 */
public class MessageHandlerRegistry {
    private MessageHandlerRegistry(){}
    private static final Map<Class<? extends AbstractMessage>,MessageHandler> container = new ConcurrentHashMap<>();
    static{
		container.put(TextMessage.class, new TextMessageHandler());
    }

    /**
	* 根据消息class获取handler,未找到返回默认handler
	* @param msgClass 消息class
	* @return
	*/
    public static MessageHandler get(Class<? extends AbstractMessage> msgClass){
	   MessageHandler messageHandler = container.get(msgClass);
	   return messageHandler ==null?new DefaultMessageHandler(): messageHandler;
    }

    public  static void put(Class<? extends AbstractMessage> msgClass , MessageHandler handler){
	   container.put(msgClass, handler);
    }

    public  static void remove(Class<? extends AbstractMessage> msgClass , MessageHandler handler){
	   container.remove(msgClass);
    }
}
